package com.moli.iov.task.remoting.op.reponse;

import com.google.common.collect.Maps;
import com.moli.iov.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName VehiclePushResponseHandler
 * @Description 向OP推送数据公共处理器
 * @Author 李伟松
 * @CreateDate 2019-03-20 下午 2:24
 * @UpdateDate 2019-03-20 下午 2:24
 * @Version 1.0
 */
@Slf4j
@Component
public class VehiclePushResponseHandler {
    @Autowired
    private TokenResponseHandler tokenRequestHandler;

    @Value("${op_channel_val}")
    private String opChannelVal;

    /**
     * 获取token，调用推送方法
     *
     * @return
     */
    public String push(String url,String body) throws IOException {
        String token = tokenRequestHandler.getToken();
        return this.pushReal(url,token, body);
    }

    /**
     * 向OP推送数据
     * @param token
     * @return
     * @throws IOException
     */
    public String pushReal(String url,String token, String body) throws IOException {
        Map<String, String> header = Maps.newHashMap();
        header.put("token", token);
        header.put("channel", opChannelVal);
        String result = HttpUtil.postJson(url, body, header);
        return result;

    }
}
